<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="Pragma" content="no-cache" />
    <title>巴蜀行</title>
</head>
<body>
<script>
    let appid = 'wxacdf899d08a60cb6',//appid
        httpUrl='https://bsx.faguikeji.com/redictWeChat.html',
        httpIndex="https://bsx.faguikeji.com/index.html#/";
    let utils = {
        apisServer: 'https://bsx.faguikeji.com',
        /*获取url的值*/
        getUrlName: function (name) {
            //当前地址信息
            console.log('location地址信息');
            console.log(location.href);
            console.log(JSON.stringify(location));
            return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
        },
        /*获取storage*/
        projectName: 'car_c',
        storage: {
            getter: function (key, type) {
                if (type == 1) {
                    return JSON.parse(sessionStorage.getItem(utils.projectName + '_' + key));
                }
                else {
                    return JSON.parse(localStorage.getItem(utils.projectName + '_' + key));
                }
            },
            setter: function (key, val, type) {
                val = JSON.stringify(val);
                if (type == 1) {
                    return sessionStorage.setItem(utils.projectName + '_' + key, val);

                } else {
                    return localStorage.setItem(utils.projectName + '_' + key, val);
                }
            },
            remove: function (key, type) {
                if (type == 1) {
                    return sessionStorage.removeItem(utils.projectName + '_' + key);
                }
                else {
                    return localStorage.removeItem(utils.projectName + '_' + key);
                }
            }
        },
        int: function (code) {
            const that=this
            //打印json数据信息
            console.log('微信code信息');
            console.log(JSON.stringify(code));
            //ajax 获取用户openid信息
            let ajax = new XMLHttpRequest();
            //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
            ajax.open('get', that.apisServer+'/getopenid?code=' + code);
            //步骤三:发送请求
            ajax.send();
             //步骤四:注册事件 onreadystatechange 状态改变就会调用
            ajax.onreadystatechange = function () {
                console.log('ajax数据');
                console.log(ajax);
                if (ajax.readyState == 4 && ajax.status == 200) {
                    //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
                    let res =ajax.responseText;
                    if(res){
                        that.storage.setter('openid',res,1);
                       // window.location.href=that.storage.getter('formUrl',1)
                        window.location.href=httpIndex;
                    }
                }
            }
        }
    }
    let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+httpUrl+'&response_type=code&scope=snsapi_base&state=1#wechat_redirect'
    let code = utils.getUrlName('code');
    console.log('code数据'+'-----------'+code);
    if (code) {
        if(utils.storage.getter('openid',1)){
            if(history.length<1){
                window.location.href=httpIndex;
            }
        }else {
            utils.int(code)
        }
    } else {
        window.location.href = url;
    }
</script>
</body>

</html>
